import 'package:flutter/material.dart';
import 'package:flutter_web_study/api/app_config.dart';
import '../provider/base_provider.dart';
import '../view_model/commodity_evaluation_view_model.dart';

///@Description 商品评价
///@systemUser HXC182
///@Author Nine Tailed Fox
///@Date 2023/1/31 17:28
class CommodityEvaluationPage extends StatefulWidget {
  const CommodityEvaluationPage({Key? key}) : super(key: key);

  @override
  State<CommodityEvaluationPage> createState() =>
      _CommodityEvaluationPageState();
}

class _CommodityEvaluationPageState extends State<CommodityEvaluationPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<CommodityEvaluationViewModel>(
        model: CommodityEvaluationViewModel(context),
        onModelReady: (value) {
          value.initView();
        },
        builder: (context, viewModel, child) {
          return Scaffold(
            appBar: AppBar(
              title: const Text("商品管理"),
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Expanded(
                  child: SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Container(
                      child: DataTable(
                        showCheckboxColumn: true,
                        dividerThickness: 2,
                        sortColumnIndex: 1,
                        columns: const [
                          DataColumn(label: Expanded(child: Text("编号"))),
                          DataColumn(label: Expanded(child: Text("是否精选"))),
                          DataColumn(label: Expanded(child: Text("商品图片"))),
                          DataColumn(label: Expanded(child: Text("商品信息"))),
                          DataColumn(label: Expanded(child: Text("用户昵称"))),
                          DataColumn(label: Expanded(child: Text("评论内容"))),
                          DataColumn(label: Expanded(child: Text("操作")))
                        ],
                        rows: _dataRowList(viewModel),
                      ),
                    ),
                  ),
                ),
                Container(
                  color: Colors.black12,
                  child: Row(
                    children: [
                      IconButton(
                          onPressed: () => viewModel.addPage(false),
                          icon: Icon(Icons.keyboard_arrow_left)),
                      Container(
                        width: 25,
                        height: 25,
                        color: Colors.blue,
                        alignment: Alignment.center,
                        child: Text("${viewModel.page1}"),
                      ),
                      Container(
                        width: 25,
                        height: 25,
                        alignment: Alignment.center,
                        color: Colors.white,
                        child: Text("${viewModel.page2}"),
                      ),
                      IconButton(
                          onPressed: () => viewModel.addPage(true),
                          icon: Icon(Icons.keyboard_arrow_right))
                    ],
                  ),
                )
              ],
            ),
          );
        });
  }

  List<DataRow> _dataRowList(CommodityEvaluationViewModel viewModel) {
    List<DataRow> rows = [];
    if (viewModel.listShopAll.length != 0) {
      viewModel.listShopAll.forEach((element) {
        rows.add(DataRow(
            onSelectChanged: (select) {
              viewModel.multipleChoice(element);
            },
            selected: viewModel.listShopAllSelect.contains(element),
            cells: [
              DataCell(SelectableText("${element["commentId"]}")),
              DataCell(SelectableText((element["isPrime"]) ? "是" : "否")),
              DataCell(Image.network(
                AppConfig.IMAGE_PREFIX + (element["goodsImage"]?[0]),
                width: 50,
              )),
              DataCell(SelectableText("${element["goodsName"]}")),
              DataCell(SelectableText("${element["uploaderName"]}")),
              DataCell(Container(
                  width: 100,
                  child: SelectableText(
                    "${element["content"]}",
                  ))),
              DataCell(Row(
                children: [
                  MaterialButton(
                      child: Text("编辑"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.editComment(element);
                      }),
                  MaterialButton(
                      child: Text("精选"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.concentration(element);
                      }),
                  MaterialButton(
                      child: Text("删除"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.deleteComment(element);
                      })
                ],
              )),
            ]));
      });
    }
    return rows;
  }
}
